package edu.whut.mall.portal.service.impl;

import edu.whut.mall.mapper.ShippingMapper;
import edu.whut.mall.model.Shipping;
import edu.whut.mall.model.ShippingExample;
import edu.whut.mall.portal.domain.ShippingParams;
import edu.whut.mall.portal.mapper.ShippingCustomMapper;
import edu.whut.mall.portal.service.IShippingService;
import edu.whut.mall.portal.vo.ShippingVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
* @program: d9lab-mall
* 收货地址 Service实现
* @author Wayne
* @date Thu May 21 15:18:17 CST 2020
*/

@Service
@RequiredArgsConstructor
public class ShippingServiceImpl implements IShippingService {


    private final ShippingMapper shippingMapper;

    private final ShippingCustomMapper shippingCustomMapper;


    @Override
    public Integer insertOneShipping(ShippingParams shippingParams) {
        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(shippingParams, shipping);
        //如果是默认地址
        if (shippingParams.getIsMain().equals(1)) {
            //把原来的默认地址设置为不是默认的
            shippingCustomMapper.revokeDefaultAddressByShippingId(shippingParams.getUserId());
        }
        return shippingMapper.insertSelective(shipping);
    }

    @Override
    public Integer updateOneShipping(ShippingParams shippingParams) {
        Shipping shipping = new Shipping();
        BeanUtils.copyProperties(shippingParams, shipping);
        shipping.setUpdateTime(new Date());
        return shippingMapper.updateByPrimaryKeySelective(shipping);
    }

    @Override
    public List<ShippingVo> listShippingVosByUserId(Long userId) {
        ShippingExample shippingExample = new ShippingExample();
        shippingExample.createCriteria().andUserIdEqualTo(userId);
        List<Shipping> shippingList = shippingMapper.selectByExample(shippingExample);
        List<ShippingVo> shippingVoList = shippingList.stream().map(this::trans2Vo).collect(Collectors.toList());
        return shippingVoList;
    }

    @Override
    public Integer deleteOneShipping(Long shippingId) {
        return shippingMapper.deleteByPrimaryKey(shippingId);
    }

    @Override
    @Transactional
    public Integer setDefaultShipping(Long userId, Long shippingId) {
        //把原来的默认地址设置为不是默认的
        shippingCustomMapper.revokeDefaultAddressByShippingId(userId);
        Shipping shipping = new Shipping();
        shipping.setId(shippingId);
        shipping.setIsMain(1);
        return shippingMapper.updateByPrimaryKeySelective(shipping);
    }

    @Override
    public Integer modifyShippingAddress(String orderNo, String username, String phone, String receiveAddress) {
        return shippingCustomMapper.modifyShippingAddress(orderNo, username, phone, receiveAddress);
    }


    private ShippingVo trans2Vo(Shipping shipping){
        ShippingVo shippingVo = new ShippingVo();
        BeanUtils.copyProperties(shipping, shippingVo);
        return shippingVo;
    }
}




